import { Toast } from 'vant';
import { COMMON_TYPES } from '@/constant';

// 查询是否存在于购物车
function goodInCart (state, payload) {
  const { re_a_id } = payload;
  const good = _.find(state.cart[getCartType(state)], { re_a_id });
  let res = {
    res: true,
    good
  };
  if (_.isUndefined(good)) res.res = false;
  return res;
}

// 获取当前购物车类型
export function getCartType ({ cartType }) {
  return cartType;
}

// 操作类型
export const types = {
  ADD_GOOD: 'ADD_GOOD',
  MINUS_GOOD: 'MINUS_GOOD',
  ADD_GOOD_COUNT: 'ADD_GOOD_COUNT',
  MINUS_GOOD_COUNT: 'MINUS_GOOD_COUNT',
  INPUT_GOOD_COUNT: 'INPUT_GOOD_COUNT',
  SWITCH_CHECKED: 'SWITCH_CHECKED',
  RESET_CART: 'RESET_CART',
  SET_TYPE: 'SET_TYPE'
};

const state = {
  cart: {
    [COMMON_TYPES.ASSET]: [], // 资产购物车
    [COMMON_TYPES.CONSUMABLE]: [] // 耗材购物车
  },
  cartType: null
};

const mutations = {
  // 添加商品
  [types.ADD_GOOD] (state, payload) {
    const { res } = goodInCart(state, payload);
    // 已经存在,判断库存
    if (res) return;
    state.cart[getCartType(state)].push({
      ...payload,
      count: 1,
      checked: true
    });
    Toast('添加成功 :)');
  },
  // 移除商品
  [types.MINUS_GOOD] (state, payload) {
    const { res } = goodInCart(state, payload);
    if (res) {
      const { re_a_id } = payload;
      const cartType = getCartType(state);
      state.cart[cartType].splice(
        _.findIndex(state.cart[cartType], { re_a_id }),
        1
      );
      Toast('移除成功 :)');
    }
  },
  // 添加商品件数
  [types.ADD_GOOD_COUNT] (state, payload) {
    const { res, good } = goodInCart(state, payload);
    // 购物车中存在切商品数小于库存
    if (res && good.count < good.c_stock) {
      const { re_a_id } = payload;
      _.find(state.cart[getCartType(state)], { re_a_id }).count++;
      Toast('添加成功 :)');
      return;
    }
    Toast('超出库存 :)');
  },
  // 减少商品件数
  [types.MINUS_GOOD_COUNT] (state, payload) {
    const { res } = goodInCart(state, payload);
    if (res) {
      // 件数为0
      const { re_a_id } = payload;
      const cartType = getCartType(state);
      const good = _.find(state.cart[cartType], { re_a_id });
      if (good.count) {
        good.count--;
        if (!good.count) {
          state.cart[cartType].splice(
            _.findIndex(state.cart[cartType], { re_a_id }),
            1
          );
        }
        Toast('移除成功 :)');
      }
    }
  },
  // 手动输入商品数量
  [types.INPUT_GOOD_COUNT] (state, payload) {
    const { res, good } = goodInCart(state, payload);
    // 购物车中存在切商品数小于库存
    if (res) {
      const { re_a_id } = payload;
      const currentGood = _.find(state.cart[getCartType(state)], { re_a_id });
      currentGood.count = payload.count <= good.c_stock ? payload.count : currentGood.c_stock;
      Toast('添加成功 :)');
    }
  },
  // 切换选中状态
  [types.SWITCH_CHECKED] (state, payload) {
    const { res, good } = goodInCart(state, payload);
    if (res) {
      good.checked = !good.checked;
    }
  },
  // 重置购物车
  [types.RESET_CART] (state) {
    const cartType = getCartType(state);
    state.cart[cartType] = state.cart[cartType].filter(item => !item.checked);
  },
  // 设置购物车类型
  [types.SET_TYPE] (state, type) {
    state.cartType = type;
  }
};

const actions = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
